package ${packageName}.controller;

import com.mdhw.config.R;
import lombok.extern.slf4j.Slf4j;
import ${packageName}.service.${entityName}Service;
import ${packageName}.entity.${entityName};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.mdhw.common.enums.StatusEnum;
import java.util.*;

/**
* @author ${author}
* @date ${date}
*/
@Slf4j
@RestController
@Api(tags = "${desc}")
@RequestMapping("${entityName?uncap_first}")
public class ${entityName}Controller {

    @Autowired
    private ${entityName}Service ${entityName?uncap_first}Service;


    @GetMapping("/list")
    @ApiOperation(value = "${desc}列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "页码", paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "单页容量", paramType = "query", dataType = "Integer")
    })
    public R<Page<${entityName}>> getList(Integer pageNo, Integer pageSize) {
        QueryWrapper<${entityName}> query = new QueryWrapper<>();
        query.lambda().eq(${entityName}::getStatus, StatusEnum.OK.getCode());
        Page<${entityName}> page = new Page<>(pageNo, pageSize);
        Page<${entityName}> pageData = ${entityName?uncap_first}Service.page(page, query);
        return R.success(pageData);
    }


    @PostMapping("/add")
    @ApiOperation(value = "${desc}新增")
    public R<String> add(@RequestBody @Validated ${entityName} data) {
        boolean save = ${entityName?uncap_first}Service.save(data);
        if (save) {
            return R.success("新增成功");
        }
        return R.error("新增失败");
    }


    @PutMapping("/update")
    @ApiOperation(value = "${desc}更新")
    public R<String> update(@RequestBody @Validated ${entityName} data) {
        boolean update = ${entityName?uncap_first}Service.updateById(data);
        if (update) {
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }


    @DeleteMapping("/delete")
    @ApiOperation(value = "${desc}删除")
    public R<String> delete(@ApiParam(name = "id",value = "数据id") @RequestParam("id")Long id) {
        ${entityName} byId = ${entityName?uncap_first}Service.getById(id);
        byId.setStatus(StatusEnum.DELETE.getCode());
        boolean update = ${entityName?uncap_first}Service.updateById(byId);
        if (update) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    @GetMapping("/info")
    @ApiOperation(value = "${desc}详情")
    public R<${entityName}> getInfo(@ApiParam(name = "id",value = "数据id") @RequestParam("id")Long id) {
        ${entityName} byId = ${entityName?uncap_first}Service.getById(id);
        return R.success(byId);
    }

}
